package com.ailight.smart.api.task;

import com.ailight.smart.api.client.Esp8266NettyClient;
import com.ailight.smart.api.entity.dto.LightGearDTO;
import com.ailight.smart.api.enums.ModelEnums;
import com.ailight.smart.api.enums.OperationTypeEnums;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RequiredArgsConstructor
public class TurnLightOffTask {
    private final Esp8266NettyClient esp8266NettyClient;

    @Scheduled(cron = "0/20 * * * * ?")
    public void executeLightOffTask() {
        // 使用cron表达式实现相同效果：每20秒执行一次
        log.info("LightOff Task---开始执行");
        // 先判断当前的状态
        if (esp8266NettyClient.isConnected()) {
            // 获取挡位以及光照
            LightGearDTO dto = esp8266NettyClient.getCurrentData();
            if (dto.getGear() > 0) {
                if (dto.isHasPeople() && esp8266NettyClient.getStateMap().equals(ModelEnums.MANUAL_M0DE)) {
                    // 关灯
                    esp8266NettyClient.sendCommand(OperationTypeEnums.ENABLE_LIGHT_OFF);
                }
            }
        }
    }
}
